/*!
 * Page: mall/publish/cityRules/CityRules
 * License: MIT
 * Created: 2019-11-23 16:09
 */

/* eslint-disable @typescript-eslint/no-object-literal-type-assertion */

Page({
  data: {
    city: [] as any,
    cityArr: [
      '北京',
      '天津',
      '河北',
      '山西',
      '内蒙古',
      '辽宁',
      '吉林',
      '黑龙江',
      '上海',
      '江苏',
      '浙江',
      '安徽',
      '福建',
      '江西',
      '山东',
      '河南',
      '湖北',
      '湖南',
      '广东',
      '广西',
      '海南',
      '重庆',
      '四川',
      '贵州',
      '云南',
      '西藏',
      '陕西',
      '甘肃',
      '青海',
      '宁夏',
      '新疆',
    ],
  },
  onLoad() {
    this.getLastPageData()
  },
  /** 获取上一页的数据 */
  getLastPageData() {
    const pages = getCurrentPages()
    const lastpage = pages[pages.length - 2]
    const city = lastpage.data.city
    const cityArr = [] as any
    city.length > 0
      ? this.data.cityArr.filter((items) => {
          if (city.find((item: any) => item == items) != items) {
            cityArr.push(items)
          }
        })
      : ''
    this.setData({
      city,
      cityArr: city.length > 0 ? cityArr : this.data.cityArr,
    })
  },
  /** 选择城市 */
  chooseCity(e: WechatMiniprogram.BaseEvent) {
    const item = e.currentTarget.dataset.item
    const cityArr = [] as any
    this.data.cityArr.forEach((city: any) => {
      if (city != item) {
        cityArr.push(city)
      }
    })
    const city = this.data.city
    city.push(item)
    this.setData({
      city,
      cityArr,
    })
  },
  /** 取消选择城市 */
  cancleCity(e: WechatMiniprogram.BaseEvent) {
    const item = e.currentTarget.dataset.item
    const city = [] as any
    this.data.city.forEach((citys: any) => {
      if (citys != item) {
        city.push(citys)
      }
    })
    const cityArr = this.data.cityArr
    cityArr.push(item)
    this.setData({
      city,
      cityArr,
    })
  },
  submit() {
    const pages = getCurrentPages()
    const lastpage = pages[pages.length - 2]
    const city = this.data.city
    lastpage.setData({
      city,
      cityString: city.toString(),
    })
    wx.navigateBack({ delta: 1 })
  },
})
